package com.hz.keyboard;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.hz.keyboard.widget.CarnoInputDialog;

public class MainActivity extends AppCompatActivity {

    private Button vCarNoWidgetCarProvince,vCarNoWidgetCarLetter;   //省份跟地区
    private Button vCarNoWidgetCarNo1,vCarNoWidgetCarNo2;
    private Button vCarNoWidgetCarNo3,vCarNoWidgetCarNo4,vCarNoWidgetCarNo5;
    private LinearLayout vLlBtnList;

    private CarnoInputDialog carnoInputDialog1;
    private CarnoInputDialog carnoInputDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        vCarNoWidgetCarProvince = (Button) findViewById(R.id.vCarNoWidgetCarProvince);
        vCarNoWidgetCarLetter = (Button) findViewById(R.id.vCarNoWidgetCarLetter);

        vCarNoWidgetCarNo1 = (Button) findViewById(R.id.vCarNoWidgetCarNo1);
        vCarNoWidgetCarNo2 = (Button) findViewById(R.id.vCarNoWidgetCarNo2);
        vCarNoWidgetCarNo3 = (Button) findViewById(R.id.vCarNoWidgetCarNo3);
        vCarNoWidgetCarNo4 = (Button) findViewById(R.id.vCarNoWidgetCarNo4);
        vCarNoWidgetCarNo5 = (Button) findViewById(R.id.vCarNoWidgetCarNo5);

        vLlBtnList = (LinearLayout) findViewById(R.id.vLlBtnList);

        carnoInputDialog = new CarnoInputDialog(this);
        carnoInputDialog1 = new CarnoInputDialog(this);
        initListener();
    }

    private void initListener() {
        vCarNoWidgetCarProvince.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carnoInputDialog1.setOnCarnoInputListener(new CarnoInputDialog.OnCarnoInputListener() {
                    @Override
                    public void back(String content) {
                        vCarNoWidgetCarProvince.setText(content);
                    }
                    @Override
                    public void delText() {
                        vCarNoWidgetCarProvince.setText("");
                    }
                });
                carnoInputDialog1.builder(0).show();
            }
        });
        vCarNoWidgetCarLetter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carnoInputDialog1.setOnCarnoInputListener(new CarnoInputDialog.OnCarnoInputListener() {
                    @Override
                    public void back(String content) {
                        vCarNoWidgetCarLetter.setText(content);
                    }
                    @Override
                    public void delText() {
                        vCarNoWidgetCarLetter.setText("");
                    }
                });
                carnoInputDialog1.builder(1).show();
            }
        });

        for (int i = 0; i < vLlBtnList.getChildCount(); i++) {
            vLlBtnList.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    selectBtnId = v.getId();
                    carnoInputDialog.builder(2).show();
                }
            });
        }

        carnoInputDialog.setOnCarnoInputListener(new CarnoInputDialog.OnCarnoInputListener() {
            @Override
            public void back(String content) {
                //点击第一个后依次把id往后面一位传
                if(selectBtnId == R.id.vCarNoWidgetCarNo1){
                    vCarNoWidgetCarNo1.setText(content);
                    if(!content.isEmpty()){  //等于空等就是删除了
                        selectBtnId = R.id.vCarNoWidgetCarNo2;
                    }
                }else if(selectBtnId == R.id.vCarNoWidgetCarNo2){
                    vCarNoWidgetCarNo2.setText(content);
                    if(!content.isEmpty()){
                        selectBtnId = R.id.vCarNoWidgetCarNo3;
                    }
                }else if(selectBtnId == R.id.vCarNoWidgetCarNo3){
                    vCarNoWidgetCarNo3.setText(content);
                    if(!content.isEmpty()){
                        selectBtnId = R.id.vCarNoWidgetCarNo4;
                    }
                }else if(selectBtnId == R.id.vCarNoWidgetCarNo4){
                    vCarNoWidgetCarNo4.setText(content);
                    if(!content.isEmpty()){
                        selectBtnId = R.id.vCarNoWidgetCarNo5;
                    }
                }else if(selectBtnId == R.id.vCarNoWidgetCarNo5){
                    vCarNoWidgetCarNo5.setText(content);
                    if(!content.isEmpty())
                    carnoInputDialog.hide();    //关闭输入键盘
                }
            }
            @Override
            public void delText() {
                //点击第一个后依次把id往后面一位传
                if(selectBtnId == R.id.vCarNoWidgetCarNo1){
                    vCarNoWidgetCarNo1.setText("");
                }else if(selectBtnId == R.id.vCarNoWidgetCarNo2){
                    vCarNoWidgetCarNo2.setText("");
                    selectBtnId = R.id.vCarNoWidgetCarNo1;
                }else if(selectBtnId == R.id.vCarNoWidgetCarNo3){
                    vCarNoWidgetCarNo3.setText("");
                    selectBtnId = R.id.vCarNoWidgetCarNo2;
                }else if(selectBtnId == R.id.vCarNoWidgetCarNo4){
                    vCarNoWidgetCarNo4.setText("");
                    selectBtnId = R.id.vCarNoWidgetCarNo3;
                }else if(selectBtnId == R.id.vCarNoWidgetCarNo5){
                    vCarNoWidgetCarNo5.setText("");
                    selectBtnId = R.id.vCarNoWidgetCarNo4;
                }
            }
        });
    }

    private int selectBtnId = 0;
}
